/**
 * Created by lighter on 2014/12/20.
 */
package Hxw3DEngine.asset.parsers.bson.vo {

import Hxw3DEngine.asset.parsers.bson.format.*;

import flash.utils.ByteArray;
import flash.utils.Endian;

public class Stream {

    public static const stream_positions:uint = 1 << 0;

    public static const stream_uvs_1:uint = 1 << 1;
    public static const stream_uvs_2:uint = 1 << 2;
    public static const stream_uvs_3:uint = 1 << 3;
    public static const stream_uvs_4:uint = 1 << 4;

    public static const stream_normals:uint = 1 << 5;
    public static const stream_tangents:uint = 1 << 6;
    public static const stream_colors_rgba:uint = 1 << 7;

    public static const stream_joint_indices:uint = 1 << 8;
    public static const stream_vertex_weights:uint = 1 << 9;
    public static const stream_indices:uint = 1 << 10;

    public static const stream_bone_0_1:uint = 1 << 11;
    public static const stream_bone_2_3:uint = 1 << 12;
	
	public static const stream_colors_rgb:uint = 1 << 13;
	
	public static const vertexComponents:Array = [
		VertexComponent.XYZ,
		VertexComponent.UV, VertexComponent.UV, VertexComponent.UV, VertexComponent.UV,
		VertexComponent.NORMAL,
		VertexComponent.TANGENT,
		VertexComponent.RGBA,
		VertexComponent.JOINT_INDEX,
		VertexComponent.JOINT_WEIGHT_3,
		null,
		VertexComponent.BONE_0_1,
		VertexComponent.BONE_2_3,
		VertexComponent.RGB
	];


    public static function getComponent(component:uint,format:VertexFormat,bonesPreVertex:uint):VertexFormat
    {
        format = format || new VertexFormat();
        if(component & stream_positions) {
            format.addComponent(VertexComponent.XYZ);
        }
        if(component & stream_uvs_1) {
            format.addComponent(VertexComponent.UV);
        }
        if(component & stream_normals) {
            format.addComponent(VertexComponent.NORMAL);
        }
        if(component & stream_tangents) {
            format.addComponent(VertexComponent.TANGENT);
        }
        if(component & stream_colors_rgba) {
            format.addComponent(VertexComponent.RGBA);
        }
        if(component & stream_colors_rgb) {
            format.addComponent(VertexComponent.RGB);
        }		
        if(component & stream_joint_indices) {
            format.addComponent(VertexComponent.JOINT_INDEX);
        }
        if(component & stream_vertex_weights) {
            if(bonesPreVertex == 1){
                format.addComponent(VertexComponent.JOINT_WEIGHT_0);
            }else if(bonesPreVertex == 2){
                format.addComponent(VertexComponent.JOINT_WEIGHT_1);
            }else if(bonesPreVertex == 3){
                format.addComponent(VertexComponent.JOINT_WEIGHT_2);
            }else if(bonesPreVertex == 4){
                format.addComponent(VertexComponent.JOINT_WEIGHT_3);
            }
        }

        if(component & stream_bone_0_1){
            format.addComponent(VertexComponent.BONE_0_1);
        }
        if(component & stream_bone_2_3){
            format.addComponent(VertexComponent.BONE_2_3);
        }
        return format;
    }


    public var components:uint;
    public var stream:ByteArray;
    public function Stream(obj:Object) {
        components = obj.components;
        stream = obj.stream.data;
    }
	
	public static function fromVector(_components:uint, _data:Vector.<Number>):Stream {
		var stream:ByteArray = null;
		
		if(_data != null){
			stream = new ByteArray();
			stream.endian = Endian.LITTLE_ENDIAN;
			for (var i:int = 0; i < _data.length;i++){
				stream.writeFloat(_data[i]);
			}
		}
		return new Stream({"components":_components,"stream":{"data":stream}});
	}
}
}
